package web.controller;

import com.alibaba.fastjson.JSONObject;
import common.bean.SearchPageResult;
import common.bean.SearchParameter;
import common.tag.easyui.DataGrid;
import common.tag.easyui.TagUtils;
import module.model.UserModel;
import module.service.SysRoleService;
import module.service.UserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.io.PrintWriter;

@Controller
@RequestMapping(value = "/admin/admin")
public class AdminManageController extends BaseController {
    public final static String prefix="admin/module/admin/";

    @Autowired
    private UserService userService;
    @Autowired
    private SysRoleService roleService;

    protected String getPrefix() {
        return prefix;
    }


    @RequestMapping(value="saveAdmin",method= RequestMethod.POST)
    @ResponseBody
    public ModelMap save(UserModel sysUser, Integer roleId){
        ModelMap map = new ModelMap();
        setFailure(map);
        try{
            if(sysUser.getUserId()==null){
                sysUser.setUserStatus(0);
                sysUser.setCreateUser(getCurAdminId());
                sysUser.setUserPassword(DigestUtils.md5Hex(sysUser.getUserPassword()));
                userService.addUser(sysUser);
                //roleService.addRoleDispatcherByObjId(sysUser.getSysUserId());
            }else {
                userService.updateUser(sysUser,getCurAdminId());
                //roleService.deleteRoleDispatcherByObjId(sysUser.getSysUserId());
                //roleService.addRoleDispatcherByObjId(sysUser.getSysUserId());
            }
            setSuccess(map);
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }

    @Override
    @RequestMapping(value = "add",method = RequestMethod.GET)
    public String form(ModelMap map){
        //map.put("roleList",roleService.findAll());
        return ADD;
    }

    @RequestMapping(value = "list",method = RequestMethod.POST)
    @ResponseBody
    public void dataList(DataGrid dataGrid, Long formId){
        int page = dataGrid.getPage();
        SearchParameter searchParameter = new SearchParameter();
        searchParameter.setPageIdx(page);
        SearchPageResult<UserModel> serviceModelSearchPageResult = userService.getUserModelListPage(searchParameter);
        dataGrid.setResults(serviceModelSearchPageResult.getResult());
        dataGrid.setTotal(serviceModelSearchPageResult.getTotal());
        response.setContentType("application/json;charset=utf-8");
        response.setHeader("Cache-Control", "no-store");
        JSONObject object = TagUtils.getJson(dataGrid);
        try {
            PrintWriter pw=response.getWriter();
            pw.write(object.toString());
            pw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value="/update/{id}")
    public String edit(Model model, @PathVariable("id") Long id){
        model.addAttribute("item", userService.getUserModelByUserId(id));
        return UPDATE;
    }

    @RequestMapping(value = "updatePassword",method = RequestMethod.GET)
    public String updatePassword(ModelMap map, Long userId){
        map.put("userModel",userService.getUserModelByUserId(userId));
        return getPrefix()+"updatePassword";
    }

    @RequestMapping(value = "updatePassword",method = RequestMethod.POST)
    @ResponseBody
    public ModelMap updatePassword(UserModel userModel){
        ModelMap map = new ModelMap();
        setFailure(map);
        try{
            userService.updatePassword(userModel.getUserId(), DigestUtils.md5Hex(userModel.getUserPassword()),getCurAdminId());
            setSuccess(map);
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }

    @RequestMapping(value = "countByLoginId",method = RequestMethod.POST)
    @ResponseBody
    public ModelMap countByLoginId(String loginId){
        ModelMap map = new ModelMap();
        if(userService.countUserNameOrUserMobile(loginId,null)==false){
            setFailure(map);
        }else{
            setSuccess(map);
        }
        return map;
    }


}
